/*
 * Copyright (C) 2012 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package com.example.google.tv.picasanotifier;

import com.google.api.client.http.HttpTransport;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.google.tv.picasanotifier.model.AlbumData;
import com.example.google.tv.picasanotifier.model.AlbumEntry;
import com.example.google.tv.picasanotifier.model.UserFeed;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Activity to display the Picasa albums along with its title and the number of
 * photos per album.
 */
public class AlbumActivity extends Activity implements OnItemClickListener {

    static ArrayList<AlbumData> mListAlbumData = new ArrayList<AlbumData>();

    private List<AlbumEntry> mAlbums;

    private String mUserName;

    private GridView mGridView;

    private ImageManager mImageManager;

    Context mContext;

    List<AlbumEntry> mAlbumEntries;

    UserFeed mUserFeed;

    final Picasa mPicasa = Picasa.get();

    HttpTransport mTransport = mPicasa.picasaInit();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mUserName = extras.getString("username");
        }
        setContentView(R.layout.album_view);
        mAlbums = mPicasa.getUserAlbums(mUserName);
        mGridView = (GridView)findViewById(R.id.picasa_album);
        mGridView.setAdapter(new ImageAdapter(this));
        mGridView.setOnItemClickListener(this);

    }

    /**
     * Adapter to display album thumbnail in a gridview
     */
    public class ImageAdapter extends BaseAdapter {

        public ImageAdapter(Context context) {
            mImageManager = new ImageManager(context);
        }

        @Override
        public int getCount() {
            return mAlbums.size();
        }

        @Override
        public Object getItem(int position) {
            return mAlbums.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        public Object fetch(String address) throws MalformedURLException, IOException {
            URL url = new URL(address);
            Object content = url.getContent();
            return content;
        }

        // display album thumbnails
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                LayoutInflater li = getLayoutInflater();
                view = li.inflate(R.layout.image_view, null);
            } else {
                view = convertView;
            }
            TextView tv = (TextView)view.findViewById(R.id.image_text);
            tv.setText(mAlbums.get(position).title + " ( Photos: "
                    + mAlbums.get(position).numPhotos + ") ");
            ImageView i = (ImageView)view.findViewById(R.id.image);
            mImageManager.showImage(mAlbums.get(position).mediaGroup.thumbnail.url, i);
            return view;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
        Intent intent = new Intent(AlbumActivity.this, PhotoActivity.class);
        intent.putExtra("albumname", mAlbums.get(pos).title);
        intent.putExtra("username", mUserName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
